AJAX to create agents and steps but its still buggy

jamesperet 11 lat temu
rodzic
commit
426bd0034d

+ 6 - 0
app/assets/javascripts/missions.js.coffee

@@ -5,4 +5,10 @@
5 5
 jQuery ->  $('form').on 'click', '.remove_fields', (event) ->    
6 6
 	$(this).prev('input[type=hidden]').val('1')    
7 7
 	$(this).closest('fieldset').hide()    
8
+	event.preventDefault()
9
+	
10
+jQuery -> $('form').on 'click', '.add_fields', (event) ->  
11
+	time = new Date().getTime()  
12
+	regexp = new RegExp($(this).data('id'), 'g')  
13
+	$(this).before($(this).data('fields').replace(regexp, time))  
8 14
 	event.preventDefault()

+ 4 - 4
app/controllers/agent_controller.rb

@@ -12,10 +12,10 @@ class AgentController < ApplicationController
12 12
     @mission_invite.status = 'accepted'
13 13
     respond_to do |format|
14 14
       if @mission_invite.save
15
-        format.html { redirect_to mission_choose_path, notice: 'Mission was accepted.' }
15
+        format.html { redirect_to missions_path, notice: 'Mission was accepted.' }
16 16
         format.json { head :no_content }
17 17
       else
18
-        format.html { redirect_to mission_choose_path, notice: 'Mission was not accepted. Please try again later.' }
18
+        format.html { redirect_to missions_path, notice: 'Mission was not accepted. Please try again later.' }
19 19
         format.json { render json: @mission.errors, status: :unprocessable_entity }
20 20
       end
21 21
     end
@@ -27,10 +27,10 @@ class AgentController < ApplicationController
27 27
     @mission_invite.status = 'denied'
28 28
     respond_to do |format|
29 29
       if @mission_invite.save
30
-        format.html { redirect_to mission_choose_path, notice: 'Mission was denied.' }
30
+        format.html { redirect_to missions_path, notice: 'Mission was denied.' }
31 31
         format.json { head :no_content }
32 32
       else
33
-        format.html { redirect_to mission_choose_path, notice: 'Mission was not denied. Please try again later.' }
33
+        format.html { redirect_to missions_path, notice: 'Mission was not denied. Please try again later.' }
34 34
         format.json { render json: @mission.errors, status: :unprocessable_entity }
35 35
       end
36 36
     end

+ 2 - 1
app/controllers/missions_controller.rb

@@ -67,10 +67,11 @@ class MissionsController < ApplicationController
67 67
         params[:mission][:mission_agents_attributes].values.each do |a|
68 68
           if a[:mission_agent_steps_attributes] != nil
69 69
             a[:mission_agent_steps_attributes].values.each do |s|
70
-              @step = MissionAgentStep.find(s[:id])
70
+              @step = MissionAgentStep.where(:id => s[:id]).first_or_create
71 71
               if s[:_destroy] == 1.to_s
72 72
                 @step.destroy
73 73
               else
74
+                @step.mission_agent_id = a[:id]
74 75
                 @step.description = s[:description]
75 76
                 @step.save
76 77
               end

+ 8 - 0
app/helpers/application_helper.rb

@@ -1,2 +1,10 @@
1 1
 module ApplicationHelper
2
+  def link_to_add_fields(name, f, association)
3
+    new_object = f.object.send(association).klass.new    
4
+    id = new_object.object_id    
5
+    fields = f.fields_for(association, new_object, index: id) do |builder|      
6
+      render(association.to_s.singularize + "_form", f: builder)    
7
+    end    
8
+    link_to(name, '#', class: "add_fields", data: {id: id, fields: fields.gsub("\n", "")})  
9
+  end
2 10
 end

+ 1 - 0
app/views/missions/_form.html.erb

@@ -12,6 +12,7 @@
12 12
     <%= f.fields_for :mission_agents, MissionAgent.order('created_at ASC').find_all_by_mission_id(@mission) do |agent_builder| %>
13 13
     		<%= render 'mission_agent_form', f: agent_builder %>
14 14
     <%end%>
15
+    <%= link_to_add_fields "Add Agent", f, :mission_agents %>
15 16
     
16 17
   </div>
17 18
 

+ 1 - 0
app/views/missions/_mission_agent_form.html.erb

@@ -8,5 +8,6 @@
8 8
 	<%= f.fields_for :mission_agent_steps, MissionAgentStep.where(:mission_agent_id => f.object.id).order(:step => :asc) do |step_builder| %>
9 9
 		<%= render 'mission_agent_step_form', f: step_builder %>
10 10
 	<%end%>
11
+	<%= link_to_add_fields "Add Step", f, :mission_agent_steps %>
11 12
 	<% @agent = @agent + 1 %>
12 13
 </fieldset>